package com.ybt.service.impl.base;

import com.ruleengine.context.IContextProperty;
import com.ruleengine.context.IWorkflowConext;
import com.ruleengine.endpoint.data.IEndpointData;
import com.ybt.context.IYBTContextConstants;
import com.ybt.message.manager.PackUnpackStrategyManager;
import com.ybt.message.unpack.IMessageUnpack;
import com.ybt.message.unpack.exception.MessageUnpackException;
import com.ybt.service.exception.ServiceException;
import com.ybt.service.impl.BlockingBaseService;

/**
 * 通用解包服务
 * @author AB033862
 * Jul 31, 2012
 */
public class UnpackService extends BlockingBaseService {

    @Override
    public void service(IWorkflowConext context, IEndpointData data)
            throws ServiceException {
        //原始报文
        IContextProperty<?> op = context
        .getContextProperty(IYBTContextConstants.ORIGINAL_DATA);
        
        // 渠道
        IContextProperty<String> channelp = context
        .getContextProperty(IYBTContextConstants.YBT_CHANNEL);
        String channel = channelp.getValue();
        
        //获取拆包实现类
        IMessageUnpack unpack = PackUnpackStrategyManager.getInstance().getMessageUnpack(channel);
        try {
            Object result = unpack.unpack(context, op.getValue());
            data.setInData(result);
        } catch (MessageUnpackException e) {
            throw new ServiceException(e);
        }
    }

}
